package com.macro.mall.tiny.modules.garden.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.garden.dto.NoteItem;
import com.macro.mall.tiny.modules.garden.model.RxNote;
import com.macro.mall.tiny.modules.garden.service.RxNoteService;
import com.macro.mall.tiny.modules.ums.dto.UmsAdminParam;
import com.macro.mall.tiny.modules.ums.model.UmsAdmin;
import com.macro.mall.tiny.modules.ums.service.UmsAdminService;
import com.macro.mall.tiny.security.util.SecurityUtils;
import com.macro.mall.tiny.utils.DateUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author macro
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/garden/note")
public class RxNoteController {


    @Resource
    private RxNoteService rxNoteService;

    @Resource
    private UmsAdminService umsAdminService;


    @ApiOperation(value = "新增笔记")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> insert(@RequestBody RxNote note) {
        note.setCreator(SecurityUtils.getCurrentUserId());
        note.setCreateTime(new Date());
        boolean save = rxNoteService.save(note);
        return CommonResult.success(save);
    }

    @ApiOperation(value = "首页随机刷新")
    @RequestMapping(value = "/random", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<?> random() {
        QueryWrapper<RxNote> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        Page<RxNote> resultPage = rxNoteService.page(new Page<>(1, 10), queryWrapper);
        List<NoteItem> collect = resultPage.getRecords().stream().map(rxNote -> {
            NoteItem item = new NoteItem();
            item.setId(rxNote.getId());
            item.setTimestamp(DateUtils.formatRelativeTime(rxNote.getCreateTime()));
            item.setContent(rxNote.getContent());
            List<String> imageUrls = Stream.of(
                            rxNote.getImg1(), // 将四个图片 URL 字段放入一个临时列表
                            rxNote.getImg2(),
                            rxNote.getImg3(),
                            rxNote.getImg4()
                    )
                    .filter(url -> url != null && !url.isEmpty()) // 过滤掉 null 和空字符串的 URL
                    .collect(Collectors.toList()); // 将过滤后的结果收集到一个新的 List 中

            // 将构建好的包含有效图片 URL 的列表设置到你的 item 对象中
            item.setImageUrls(imageUrls);

            QueryWrapper<UmsAdmin> adminQueryWrapper = new QueryWrapper<>();
            adminQueryWrapper.eq("id", rxNote.getCreator());
            UmsAdmin one = umsAdminService.getOne(adminQueryWrapper);

            item.setName(one.getNickName());
            item.setHandle(" @" + one.getUsername());
            item.setAvatarUrl(one.getIcon());

            return item;
        }).collect(Collectors.toList());
        System.out.println(collect);

        return CommonResult.success(collect);
    }

}